#include "ctrl.h"
#include "n32wb03x.h"
#include "string.h"
#include "spi.h"
#include "store.h"
#include "ns_ble_task.h"

//// 0x8000~0x9000 FLAG 1
//// 0x9000~0x10000 FALG 2
//// 0x10000 ~ 0x40000  3*16 ///TODO 3*7 = 21;


#define  ZERO_SIZE    1000
#define  BUFFER_SIZE  (2736 + ZERO_SIZE)
uint8_t SPI_Master_Tx_Buffer[BUFFER_SIZE] = {0};

struct act_led_ctrl
{
    uint8_t green[8];
    uint8_t red [8];
    uint8_t blue [8];
};

struct led_ctrl
{
    uint8_t num;
    uint8_t green;
    uint8_t red;
    uint8_t blue;
};

struct led_picture 
{
    uint16_t length;
    struct led_ctrl led[LED_NUM];
};
struct act_led_ctrl * led_raw_data = (void *)(SPI_Master_Tx_Buffer + ZERO_SIZE);

static uint8_t display = 0;
void fillin_data(uint8_t * addr)
{
    memset(SPI_Master_Tx_Buffer, 0x00, BUFFER_SIZE);
    memset(led_raw_data, 0x80, 2736);
    
    display++;
    
    for(int i= 0; i<LED_NUM; i++)
    {
//        printf("red %2x, green%2x, blue%2x\r\n", addr[i*3 + 0], addr[i*3 + 1], addr[i*3 + 2]);
        for(int j = 0; j<8; j++)
        {
            led_raw_data[i].green[j]    = addr[i*3 + 0] & (1<<j) ?   0xE0: 0x80;
            led_raw_data[i].red[j]      = addr[i*3 + 1]  & (1<<j) ?  0xE0: 0x80;
            led_raw_data[i].blue[j]     = addr[i*3 + 2]  & (1<<j) ?  0xE0: 0x80;
//            printf("%x",led_raw_data[i].red[j]);
//            printf("%x",led_raw_data[i].blue[j]);
//            printf("%x ",led_raw_data[i].green[j]);
        }
//        printf("\r\n");
    }                        
}  


uint16_t dp_mov_index = 0;
uint16_t dp_mov_pcsn = 0;
uint16_t dp_mov_crpc = 0;

void pic_write(void)
{
    user_spis_write((uint32_t)&SPI_Master_Tx_Buffer, BUFFER_SIZE);
}    

void led_test_mode(void);
void pic_act(void)
{
    if(strat_store_process)
    {
        return;
    }   
//    printf("display\r\n");
    
    if(user_display_flag == 0  )
    {
        const_dislay_array[user_display_num]();
        return;
    } 
    else if(user_display_flag == 2 )
    {
        led_test_mode();
        return;
    }   
    // printf("DISPLAY INDEX:%x  PCS_N:%x PCS_I:%x", dp_mov_index, dp_mov_pcsn, dp_mov_crpc);

    uint8_t * act_data = 0;
    uint16_t  act_time = 0;
    ///seek for pict
    for(int i = 0; i<SECTION_NUM_AREA; i++)
    {
        if((SECTOR_VOIDF(i) == VOIDF_IND) && (SECTOR_USEDF(i) != USEDF_FRE))
        {
            if(SECTOR_INDEX(i) == dp_mov_index)
            {
                // printf("[0x%05x] INDEX:%x PCS_N:%x\r\n", i*0x10000, SECTOR_INDEX(i), SECTOR_PCS_N(i));
                for(int j = 0; j<11; j++)
                {
                    // printf("cr %x sr %x\r\n",dp_mov_crpc, SECTOR_PIC_ND(i, j));
                    if(SECTOR_PIC_ND(i, j) == dp_mov_crpc)
                    {    
                        // printf("picture[%d] time %x\r\n", SECTOR_PIC_ND(i, j),  SECTOR_PIC_TD(i, j) );
                        act_data = SECTOR_PIC_DA(i, j);
                        act_time = SECTOR_PIC_TD(i, j);
                        // for(int k = 0; k<LED_NUM*3; k++)
                        // {
                        //     if( (k%16) == 0)
                        //     {
                        //         printf("\r\n");
                        //     }    
                        //     printf("%02x ", act_data[k]);
                        // }
                        // printf("\r\n");
                    }    
                }
            }    
        }  
    }
    fillin_data(act_data);
    
    if(act_time != 0xFF)
    {    
        if(act_time == 0)
        {
            ke_timer_set(APP_PIC_TIMER, TASK_APP, 17);
        }
        else
            ke_timer_set(APP_PIC_TIMER, TASK_APP, act_time*33);
    }
    else
    {
        printf("keep pic\r\n");
    }
    
    user_spis_write((uint32_t)&SPI_Master_Tx_Buffer, BUFFER_SIZE);
    
    dp_mov_crpc++;
    
    if(dp_mov_crpc >= dp_mov_pcsn)
    {
        dp_mov_crpc = 0;
    }    
} 


void mov_act(uint16_t  index)
{
    GPIO_SetBits(GPIOB, GPIO_PIN_12);

    dp_mov_index = index;
    for(int i =0; i<SECTION_NUM_AREA; i++)
    {
        if((SECTOR_VOIDF(i) == VOIDF_IND) && (SECTOR_USEDF(i) != USEDF_FRE))
        {
            if(SECTOR_INDEX(i) == index)
            {
                dp_mov_pcsn = SECTOR_PCS_N(i);
            }    
        }    
    }
    for(int i = 0;i<10; i++)
    {
        if(movs.mov_index[i] == index)
        {
            user_display_flag = 1;
            user_display_num = i;
            break;
        }    
    }
    dp_mov_crpc = 0;
    pic_act();
}    

void mov_switch(void)
{
    printf("change_mod\r\n");
    if(user_display_flag == 2)
    {
       user_display_flag = 0; 
       user_display_num = 0;
    }    
    
    if(user_display_flag == 0)
    {
//        uint8_t found = 0;
        if( (user_display_num+1 < CONST_MAX) && const_dislay_array[user_display_num+1] != NULL)
        {
            user_display_num = user_display_num+1;
            const_dislay_array[user_display_num]();
        }    
        else
        {      
            update_movs_index();   
            if(movs.mov_index[0] != 0xFFFF)
            {
                user_display_flag = 1;
                user_display_num = 0;
                mov_act(movs.mov_index[0]);
            }
            else
            {
                user_display_flag = 0;
                user_display_num = 0;
                const_dislay_array[0]();
            }
        }
    }   
    else
    {      
        if(movs.mov_index[user_display_num+1] == 0xFFFF)
        {
            user_display_flag = 0;
            user_display_num = 0;
            const_dislay_array[0]();
        }
        else
        {
            user_display_num = user_display_num+1;
            printf("user_display_num = %x", user_display_num);
 
            printf("disply idex = %x", movs.mov_index[user_display_num+1]);
            mov_act(movs.mov_index[user_display_num]);
        }    
    }
    printf("user_display_flag =%x\r\n", user_display_flag);
    printf("user_display_num =%x\r\n", user_display_num);
}    



